
const { resolve } = require('path');

// csst提取和兼容性处理
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

// css压缩处理 两个都可以 第二个适合新版本
// const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');

// html处理
const HtmlWebpackPlugin = require('html-webpack-plugin');

// 定义node.js环境变量, 决定browlist的哪个环境
process.env.NODE_ENV = 'production';

// 复用css loader
const commonCssLoader = [
    MiniCssExtractPlugin.loader,
    'css-loader',
    // 在文件开头定义环境
    // 定义node.js环境变量, 决定使用browelist的哪个环境
    // process.env.NODE_ENV = 'production';
    // 还需要在package.json中添加规则
    /* 
    "browserslist": {
        // 开发环境 --> 设置node环境变量 process.env.NODE_ENV = development
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ],
        // 默认是看生产环境
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ]
    }
    */
    {
        loader: 'postcss-loader',
        options: {
            postcssOptions: {
                // postcss的插件
                plugins: [
                    require('postcss-preset-env')(),
                ]

            }
        }
    }
];

module.exports = {
    entry: '.src/js/index.js',
    output: {
        filename: 'js/built.js',
        path: resolve(__dirname, 'build')
    },
    module: {
        rules: [
            // css
            {
                test: /\.css$/,
                use: [...commonCssLoader]
            },
            // less
            {
                test: /\.less$/,
                use: [...commonCssLoader, 'less-loader']
            },
            // js
            {
                // js兼容性处理
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                options: {
                    presets: [
                        [
                            '@babel/preset-env',
                            {
                                useBuiltIns: 'usage',
                                corejs: { version: 3 },
                                targets: {
                                    chrome: '60',
                                    firefox: '60',
                                    ie: '9',
                                    safari: '10',
                                    edge: '17'
                                }
                            }
                        ]
                    ]
                }
            },
            // 在package.json中eslintConfig -->airbnb
            /* 
            "eslintConfig": {
                "extends": "airbnb-base"
            } 
            */
            {
                // js代码规范
                test: /\.js$/,
                exclude: /node_modules/,
                // 优先执行
                enforce: 'pre',
                loader: 'eslint-loader',
                options: {
                    fix: true
                }
            },
            // 图片
            {
                test: /\.(png|jpg|gif)$/,
                loader: 'url-loader',
                options: {
                    limit: 8 * 1024,
                    name: '[hash:10].[ext]',
                    outputPath: 'imgs',
                    esModule: false
                },
                type: 'javascript/auto'
            },
            {
                // 处理html内的图片问题
                test: /\.html$/,
                loader: 'html-loader',
            },
            // 其他文件处理, 如font 5.0不加也能自行处理
            {
                exclude: /\.(js|css|less|html|jpg|png|gif)/,
                loader: 'file-loader',
                options: {
                    name: '[hash:10].[ext]',
                    outputPath: 'media',
                    esModule: false
                },
                type: 'javascript/auto'
            }
        ]
    },
    plugins: [
        // css提取
        new MiniCssExtractPlugin({
            filename: 'css/built.css',
        }),
        // css压缩
        new CssMinimizerWebpackPlugin(),
        // html处理
        new HtmlWebpackPlugin({
            template: './src/index.html',
            minify: {
                collapseWhitespace: true,
                removeComments: true
            }
        }),
    ],
    mode: 'production',
}